import { ref, computed } from 'vue'
import type { LanguageOption } from '../api/drama'

export function useLanguageSelection(initialSourceLanguage = 'chinese') {
  const availableLanguages = ref<LanguageOption[]>([])
  const selectedLanguages = ref<string[]>([])
  const sourceLanguage = ref(initialSourceLanguage)
  const includeSourceLanguage = ref(false)

  const filteredTargetLanguages = computed(() => {
    if (includeSourceLanguage.value) {
      return availableLanguages.value
    }
    return availableLanguages.value.filter((lang: LanguageOption) => 
      lang.countryLanguage !== sourceLanguage.value
    )
  })

  const selectAll = () => {
    selectedLanguages.value = filteredTargetLanguages.value.map(lang => lang.countryLanguage)
  }

  const clearAll = () => {
    selectedLanguages.value = []
  }

  const updateAvailableLanguages = (languages: LanguageOption[]) => {
    availableLanguages.value = languages
  }

  const reset = () => {
    selectedLanguages.value = []
    sourceLanguage.value = initialSourceLanguage
    includeSourceLanguage.value = false
  }

  return {
    availableLanguages,
    selectedLanguages,
    sourceLanguage,
    includeSourceLanguage,
    filteredTargetLanguages,
    selectAll,
    clearAll,
    updateAvailableLanguages,
    reset
  }
}